package com.ocom.human.controller;


import com.ocom.common.constant.HumanServiceUrl;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.human.FeatureRequest;
import com.ocom.common.request.human.UpdateUserFaceVo;
import com.ocom.common.request.wx.UserInfoPageUpdateUserRequest;
import com.ocom.common.request.wx.UserPageAddUserRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.human.GetPersoDecryVo;
import com.ocom.common.vo.human.PersonnelGetPersoDecryVo;
import com.ocom.human.service.PersonnelFeatureService;
import com.ocom.human.service.PersonnelInfoService;
import com.ocom.human.service.RoommateInfoService;
import com.ocom.human.service.WechatPerInfoService;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Slf4j
@RestController
public class PersonController {

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    PersonnelInfoService personnelInfoService;

    @Autowired
    WechatPerInfoService wechatPerInfoService;

    @Autowired
    private RoommateInfoService roommateInfoService;

    @Autowired
    private PersonnelFeatureService personnelFeatureService;

    @PostMapping(HumanServiceUrl.URLP_existPerson)
    public Result existPerson(Long companyId, String phone,String idCard){
        boolean b = personnelInfoService.existPerson(companyId,phone,idCard);
        if(!b){
            return ResultUtil.success();
        }else{
            return ResultUtil.error(ResultEnum.Err_3015);
        }
    }

//
//    @PostMapping(HumanServiceUrl.URLP_getPersonInfo)
//    public Result getPersonInfo(Long companyId,@RequestParam(required = false) Long deptId){
//        List<EntityPersonnelInfo>  list = personnelInfoService.GetPersonInfo(companyId,deptId);
//        return ResultUtil.success(list);
//    }


//    @PostMapping(HumanServiceUrl.URLP_getPersonInfoById)
//    public Result getPersonInfoById(Long companyId,Long pId){
//        log.info("comId{{}}",companyId,"pId{{}}",pId);
//        UserInfoYkmVo personInfoById = personnelInfoService.getPersonInfoById(companyId, pId);
//        if(personInfoById==null){
//            return ResultUtil.error(ResultEnum.Err_303);
//        }
//        return ResultUtil.success(personInfoById);
//    }

    @PostMapping(HumanServiceUrl.URLP_updateUserPage)
    public Result  updateUser(@RequestBody UserInfoPageUpdateUserRequest request){
        EntityPersonnelInfo entityPersonnelInfo=personnelInfoService.updateUser(request.getCompanyId(),request);
        return ResultUtil.success(entityPersonnelInfo);
    }

    @PostMapping(HumanServiceUrl.URLP_updateUserState)
    public Result  updateUserStatus(Long companyId,Long pId,Integer status){
        EntityPersonnelInfo entityPersonnelInfo=personnelInfoService.updateUserStatus(companyId,pId,status);
        redisUtil.del("gzhToken:" + companyId + ":" + pId);
        return ResultUtil.success(entityPersonnelInfo);
    }

    @PostMapping(HumanServiceUrl.URLP_addPerUser)
    public Result  addPerUser(@RequestBody  UserPageAddUserRequest request){
        EntityPersonnelInfo entityPersonnelInfo=personnelInfoService.addPerUser(request.getCompanyId(),request);
        return ResultUtil.success(entityPersonnelInfo);
    }


    @PostMapping(HumanServiceUrl.URLP_removePerson)
    public Result  removePerson(Long companyId,Long pId,Long userId){
        log.info("companyId:{{}},pId:{{}},userId{{}}",companyId,pId,userId);
        wechatPerInfoService.removePerson(companyId,userId);
        boolean b=personnelInfoService.removePerson(companyId,pId,userId);
        if(b){
            return ResultUtil.success();
        }
        try {
            roommateInfoService.RemoveRoommate(companyId,userId,null);
        }catch (Exception exception){
            log.error("删除同住人员失败:{{}}",exception.getMessage());
        }
        redisUtil.del("gzhToken:" + companyId + ":" + pId);
        return ResultUtil.error(ResultEnum.Err_3017);
    }

    /**
     * 根据手机号身份证获取人员信息 getPersonInfoByPhoneOrCard
     * */
    @PostMapping(HumanServiceUrl.URL_GetPersonInfoByPhoneOrCard)
    public Result  GetPersonInfoByPhoneOrCard(@Validated @RequestBody GetPersoDecryVo getPersoDecryVo){
        log.info("getPersoDecryVo{{}}",getPersoDecryVo);
        if (getPersoDecryVo.getComId()==null){
            log.error("单位编号不能为空");
            return ResultUtil.error(ResultEnum.Err_203,"单位编号不能为空");
        }
        if(CommonUtil.isNull(getPersoDecryVo.getPPhone()) && CommonUtil.isNull(getPersoDecryVo.getPIdentity()) ){
            return ResultUtil.error(ResultEnum.Err_203);
        }

//        String key = MD5.MD5Encode(getPersoDecryVo.getComId().toString(), "utf8");
        PersonnelGetPersoDecryVo personnelGetPersoDecryVo = new PersonnelGetPersoDecryVo();

        personnelGetPersoDecryVo.setComId(getPersoDecryVo.getComId());

//        if (getPersoDecryVo.getPIdentity() != null && !getPersoDecryVo.getPIdentity().isEmpty()) {
//            personnelGetPersoDecryVo.setEncryIdentity(AES.encrypt(getPersoDecryVo.getPIdentity(), key));
            personnelGetPersoDecryVo.setPIdentity(getPersoDecryVo.getPIdentity());
//        }

//        if (getPersoDecryVo.getPPhone() != null && !getPersoDecryVo.getPPhone().isEmpty()){
//            personnelGetPersoDecryVo.setEncryPhone(AES.encrypt(getPersoDecryVo.getPPhone(), key));
            personnelGetPersoDecryVo.setPPhone(getPersoDecryVo.getPPhone());
//        }

//        EntityPersonnelInfo entityPersonnelInfo = null;
//        entityPersonnelInfo = personnelInfoService.PersonnelGetPersoDecry(getPersoDecryVo.getComId(), personnelGetPersoDecryVo);
//        if (entityPersonnelInfo==null){
//           entityPersonnelInfo = personnelInfoService.GetPersonInfo(getPersoDecryVo.getComId(), personnelGetPersoDecryVo);
//        }

        EntityPersonnelInfo  entityPersonnelInfo = personnelInfoService.GetPersonInfo(getPersoDecryVo.getComId(), personnelGetPersoDecryVo);

        return ResultUtil.success(entityPersonnelInfo);

    }

    /**
     * 更改人员人脸图片（/updateUserFace）
     * */
    @PostMapping(HumanServiceUrl.URL_updateUserFace)
    public Result  updateUserFace(@Validated @RequestBody UpdateUserFaceVo updateUserFaceVo){
        log.info("更改人员人脸图片请求参数:{{}}",updateUserFaceVo);
        try {

            feature(updateUserFaceVo);

            personnelInfoService.updateUserFace(updateUserFaceVo.getComId(),updateUserFaceVo);

        }catch (Exception exception){
            log.error("更改人员人脸图片失败:{{}}",exception.getMessage());
        }
        return ResultUtil.success();
    }

    private void feature(UpdateUserFaceVo updateUserFaceVo){
        try {
            EntityPersonnelInfo userFace = personnelInfoService.getUserFace(updateUserFaceVo.getComId(), updateUserFaceVo);
            if (StringUtils.isNotBlank(updateUserFaceVo.getPPhotoUrl())) {
                FeatureRequest featureRequest = new FeatureRequest();
                featureRequest.setComId(userFace.getComId());
                featureRequest.setPId(userFace.getPId());
                featureRequest.setUrl(updateUserFaceVo.getPPhotoUrl());
                featureRequest.setUrl2(null);
                personnelFeatureService.feature(userFace.getComId(), featureRequest);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
